package code1_100.code21_30;

import java.util.PriorityQueue;

/**
 * @author 韩迪
 * @date 2022/03/21 22:41
 **/
public class Code23 {
    private class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode mergeKLists(ListNode[] lists) {
        //用优先级队列求解
        PriorityQueue<ListNode> queue = new PriorityQueue<>((x,y)->x.val-y.val);
        for (int i = 0; i < lists.length; i++) {
            ListNode node = lists[i];
            while (node!=null){
                queue.offer(node);
                node = node.next;
            }
        }
        ListNode head = new ListNode();
        ListNode temp = new ListNode();
        head = temp;
        while (!queue.isEmpty()){
            temp.next = queue.poll();
            temp=temp.next;
        }
        temp.next = null;
        return head.next;
    }


}
